/*
 * @Author: 大朱 243469235@qq.com
 * @Date: 2024-08-13 09:57:44
 * @LastEditors: 大朱 243469235@qq.com
 * @LastEditTime: 2024-08-14 11:05:53
 * @FilePath: \smart-electrical-box-nest\src\app\user\user.controller.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import {
  Get,
  Controller,
  Post,
  Body,
  Query,
  UseGuards,
  Param,
  Req,
} from '@nestjs/common';
import { UserService } from './user.service';
import { Roles } from '../../auth/roles.decorator';
import { RolesGuard } from '../../auth/roles.guard';
import { PgaeDto, SetClientUserInfoDto, CreateToAdminDto } from './user.dto';
import { Request, Response } from 'express';

@Controller('user')
export class UserController {
  constructor(private readonly userService: UserService) {}

  /**
   * @description: 创建用户信息
   * @param {type}
   */
  @Post('createToAdmin')
  createToAdmin(@Body() body: CreateToAdminDto): Promise<object> {
    return this.userService.createToAdmin(body);
  }

  /**
   * @description: 修改用户信息
   * @param {type}
   */
  @Post('put/:id')
  putToAdmin(
    @Param('id') id: string,
    @Body() body: CreateToAdminDto,
  ): Promise<object> {
    return this.userService.putToAdmin(Number(id), body);
  }

  /**
   * @description: 获取用户信息
   * @param {type}
   */
  @Get()
  clientUserbyOpenid(@Query() query): Promise<object> {
    return this.userService.clientUserbyOpenid(query.openid);
  }

  /**
   * @description: 手机号授权登录
   * @param {type}
   */
  @Post('/loginByPhone')
  loginByPhone(@Body() body): Promise<object> {
    return this.userService.loginByPhone(body.openid, body.code);
  }

  /**
   * @description: 写入用户信息
   * @param {type}
   */
  @Post('/setClientInfo')
  setClientUserInfo(@Body() body: SetClientUserInfoDto): Promise<object> {
    return this.userService.setClientUserInfo(
      body.openid,
      body.userAvatarUrl,
      body.userName,
    );
  }

  /**
   * @description: 获取是否为新用户
   * @param {type}
   */
  @Get('/whetherNewUser')
  whetherNewUser(@Query('openid') openid: string): Promise<object> {
    return this.userService.whetherNewUser(openid);
  }

  /**
   * @description: 获取列表
   * @param {type}
   */
  @Roles('admin')
  @UseGuards(RolesGuard)
  @Get('/getList')
  getUserList(@Query() query: PgaeDto): Promise<object> {
    return this.userService.getUserList(query);
  }

  /**
   * @description: 修改用户信息
   * @param {type}
   */
  @Roles('admin')
  @UseGuards(RolesGuard)
  @Post('putPassword/:id')
  putPassword(@Param('id') id: string, @Body() body): Promise<object> {
    return this.userService.putPassword(Number(id), body.password);
  }

  /**
   * @description: 修改用户信息
   * @param {type}
   */
  @Roles('admin')
  @UseGuards(RolesGuard)
  @Get('info')
  getUserInfo(@Req() req: Request): Promise<object> {
    let userInfo = req['userInfo'];
    return this.userService.getUserInfo(Number(userInfo.userId));
  }
}
